package com.lebang.entity;

import com.alibaba.fastjson.JSON;
import com.lebang.common.Util;
import com.lebang.component.RedisClient;
import com.lebang.domain.MemberCashWithdrawal;
import com.lebang.domain.MemberHeaderHistory;
import com.lebangweb.common.TextUtil;
import com.lebangweb.common.UUIDUtil;
import com.lebangweb.encrypt.MessageDigestUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.mapping.Field;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.TreeMap;

/**
 * @Author: hwq
 * @Date: 2019/5/29 14:08
 * @Version 1.0
 */
@Data
@Slf4j
@Component
public class RequestHeaders {
    @Autowired
    private RedisClient redisClient;
    @Field
    private String appid;
    @Field
    private String appStore;
    @Field
    private String brand;
    @Field
    private String imei;
    @Field
    private String lat;
    @Field
    private String lng;
    @Field
    private String macAddr;
    @Field
    private String os;
    @Field
    private String osv;
    @Field
    private String sign;
    @Field
    private String time;
    @Field
    private String userid;
    @Field
    private String version;
    @Field
    private String servletPath;
    @Field
    private String ipAddr;
    @Field
    private String params;
    @Field
    private String sessionId;
    @Field
    private String ua;
    @Field
    private String appSecret;
    private String oldImei;
    private String osVersion;
    private String oaid;
    private String android_id;


    public RequestHeaders() {
    }

    public RequestHeaders(HttpServletRequest request) {
        this.appid = request.getHeader("appid") == null ? "xianyu" : request.getHeader("appid");
        this.appStore = request.getHeader("appStore");
        this.version = request.getHeader("version");
        this.imei = request.getHeader("imei") == null ? "NoneImei:" + UUIDUtil.NS() : request.getHeader("imei");
        this.oldImei = request.getHeader("imei");
        this.brand = request.getHeader("brand");
        this.os = request.getHeader("os");
        this.osv = request.getHeader("osv");
        this.lat = request.getHeader("lat");
        this.lng = request.getHeader("lng");
        this.macAddr = request.getHeader("macAddr");
        this.userid = request.getHeader("userid");
        this.time = request.getHeader("time");
        this.sign = request.getHeader("sign");
        this.servletPath = request.getServletPath();
        this.ipAddr = Util.getIpAddress(request);
        this.params = JSON.toJSONString(request.getParameterMap());
        this.ua = request.getHeader("ua") == null ? "" : request.getHeader("ua");
        this.appSecret = request.getHeader("appSecret") == null ? "" : request.getHeader("appSecret");
        this.osVersion = request.getHeader("os_version") == null ? "" : request.getHeader("os_version");
        this.oaid = request.getHeader("oaid") == null ? "" : request.getHeader("oaid");
        this.android_id = request.getHeader("android_id") == null ? "" : request.getHeader("android_id");
    }

    public boolean checkSign() {
        // 校验签名
        TreeMap<String, String> param = new TreeMap<String, String>();
        param.put("appid", appid);
        param.put("version", version);
        param.put("appStore", appStore);
        param.put("brand", brand);
        param.put("osv", osv);
        param.put("imei", oldImei);
        param.put("macAddr", macAddr);
        param.put("os", os);
        param.put("lat", lat);
        param.put("lng", lng);
        param.put("userid", userid);
        param.put("time", time);
        param.put("appsecret", "lebang8888!!");

        StringBuilder sb = new StringBuilder();
        for (String key : param.keySet()) {
            String val = param.get(key);
            if (val != null && !TextUtil.isEmpty(val)) {
                if (sb.length() > 0) {
                    sb.append("&");
                }
                sb.append(key).append("=").append(param.get(key));
            }
        }
        String requestSign = null;
        try {
            requestSign = MessageDigestUtil.encryptMD5((sb.toString().getBytes()));
            if (!requestSign.equals(this.sign)) {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

        return true;
    }
    public RequestHeaders (MemberCashWithdrawal header) {
        this.appid = "xianyu";
        this.appStore = header.getAppStore();
        this.version = header.getVersion();
        this.imei = header.getImei();
        this.oldImei = header.getImei();
        this.brand = header.getAppStore();
        this.macAddr = header.getMacAddr();
        this.userid = header.getMemberId();
        this.ipAddr = header.getIpAddr();
    }

    public int getVersionInt(){
        try {
            return Integer.parseInt(version);
        }catch (Exception e){
            return 100;
        }

    }

    public RequestHeaders(MemberHeaderHistory memberHeaderHistory) {
        this.appid = memberHeaderHistory.getAppId();
        this.appStore = memberHeaderHistory.getAppStore();
        this.brand = memberHeaderHistory.getBrand();
        this.imei = memberHeaderHistory.getImei();
        this.lat = memberHeaderHistory.getLat();
        this.lng = memberHeaderHistory.getLng();
        this.macAddr = memberHeaderHistory.getMacAddr();
        this.os = memberHeaderHistory.getOs();
        this.osv = memberHeaderHistory.getOsv();
        this.userid = memberHeaderHistory.getMemberId();
        this.version = memberHeaderHistory.getVersion()==null?"100":memberHeaderHistory.getVersion().toString();
        this.ipAddr = memberHeaderHistory.getIpAddr();
        this.ua = memberHeaderHistory.getUa();
        this.osVersion = memberHeaderHistory.getOsVersion();
        this.oaid = memberHeaderHistory.getOaId();
        this.android_id = memberHeaderHistory.getAndroidId();
    }



}
